package com.klb.action.catalog;

import com.klb.service.catalog.impl.BlockServiceCatalogImpl;
import com.klb.service.catalog.impl.PublishServiceCatalogImpl;
import com.klb.tools.PostValue;
import com.klb.tools.Sessions;
import com.klb.tools.Tools;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fujia on 2016/6/7.
 */
public class PublishActionCatalog extends ActionSupport {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int blockId;
    private int userId;
    private String title;
    private String text;
    
    private HashMap<String,Object> data;

    private BlockServiceCatalogImpl blockServiceCatalog;
    private PublishServiceCatalogImpl publishServiceCatalog;

    

    public int getBlockId() {
		return blockId;
	}
	public void setBlockId(int blockId) {
		this.blockId = blockId;
	}

	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}


	public void setBlockServiceCatalog(BlockServiceCatalogImpl blockServiceCatalog) {
		this.blockServiceCatalog = blockServiceCatalog;
	}


	public void setPublishServiceCatalog(PublishServiceCatalogImpl publishServiceCatalog) {
		this.publishServiceCatalog = publishServiceCatalog;
	}

	@Override
    public String execute() throws Exception {
        List<String> title = this.blockServiceCatalog.getAllTitle();

        Sessions.setSession("selectBlock", title);

        return SUCCESS;
    }

    public String postArticle(){
        if (this.publishServiceCatalog.publishArticleIsSuccess(this.blockId, this.userId, this.title, this.text))
            return SUCCESS;

        return ERROR;
    }

    public String jsonForPublish(){
        JSONObject jsonObject = PostValue.getJsonObject();

        if (!Tools.objectIsNullOrNot(jsonObject)){
            int blockIdP = (int) jsonObject.get("blockId");
            int userIdP = (int) jsonObject.get("userId");
            String titleP = (String) jsonObject.get("title");
            String textP = (String) jsonObject.get("text");

            this.data = new HashMap<>();

            if (this.publishServiceCatalog.publishArticleIsSuccess(blockIdP, userIdP, titleP, textP))
                this.data.put("success", true);
            else
                this.data.put("success", false);

        } else {
            this.data = null;
        }

        return SUCCESS;
    }

    public Map<String, Object> getData() {
        return data;
    }


}
